import { nextTick, ref } from 'vue'
import { getImgsAPI } from '@/services/imgs'
import _ from 'lodash'
export default () => {

    const top: any = ref(0)
    const paging: any = ref(null)
    const oldTop: any = ref(0)
    const move: any = ref(0)
    const show: any = ref(true)
    const imgList = ref<string[]>([])
    let timer: any = null
    const triggered = ref(false)
    const imgsScroll = _.throttle((e: any) => {
        oldTop.value = e.detail.scrollTop
        move.value = 60
        clearTimeout(timer)
        timer = setTimeout(() => {
            move.value = 0
        }, 500)
        if (e.detail.scrollTop > 200) {
            show.value = false
        } else {
            show.value = true
        }
    }, 200)

    const getImgList = async (id: string, num: number, all: boolean = false) => {
        const ids = ['sjpic', 'img1', 'img2', 'pc', 'acghs', 'acgbs', 'kemonomimi', 'yin', 'loli']
        // 随机数
        const random = Math.floor(Math.random() * ids.length)
        const obj = {
            id: all != false ? ids[random] : id,
            num: num,
            size: "bmiddle"
        }
        try {
            const res = await getImgsAPI(obj)
            for (let i = 0; i < res.pic.length; i++) {
                const exit = imgList.value.includes(res.pic[i])
                if (!exit) {
                    imgList.value.push(res.pic[i])
                }
            }
            setTimeout(() => {
                triggered.value = false
                console.log(triggered.value);
            }, 1000)
        } catch (error) {
            triggered.value = false
        }
    }




    const viewImg = (url: string) => {
        const largeUrl = url.replace("bmiddle", "large")
        // uni.previewImage({
        //     urls: urls,
        //     current: url
        // })
        uni.navigateTo({
            url: `/pages/imgDetail/imgDetail?url=${largeUrl}`
        })
    }


    return {
        top,
        paging,
        oldTop,
        show,
        move,
        imgList,
        imgsScroll,
        triggered,
        getImgList,
        viewImg
    }
}
